package task

import (
	"fmt"
	"net/http"
	"teamJDI/database"
	"teamJDI/util"

	"github.com/gin-gonic/gin"
)

func helloHandler(c *gin.Context) {
	var user User
	var users []User
	database.DB.First(&user)
	database.DB.Find(&users)
	fmt.Println(user)
	fmt.Println(users)
	for _, u := range users {
		fmt.Print(u.ID)
		fmt.Print(u.Name)
		fmt.Print(u.Nick)
		fmt.Print(u.JoinDate)
	}
	c.JSON(http.StatusOK, gin.H{
		"message": "Hello q1mi!",
	})
}

func teamTaskHandler(c *gin.Context) {
	var tasks []Task
	user_id := c.Param("user_id")
	err := database.DB.Where(map[string]interface{}{"HandlerID": user_id}).Find(&tasks)
	fmt.Print(err)
	c.JSON(http.StatusOK, gin.H{
		"task_id": 1,
	})
}

// 添加任务, 请求参数
type TaskForm struct {
	ParentTaskID int            `json:"parent_task_id"`
	Content      string         `json:"content" binding:"required"`
	DegreeID     int            `json:"degree_id" binding:"required"`
	CreateorID   int            `json:"createor_id" binding:"required"`
	HandlerID    int            `json:"handler_id" binding:"required"`
	StartDate    util.LocalTime `json:"start_date" binding:"required"`
	EndDate      util.LocalTime `json:"end_date" binding:"required"`
	TeamID       int            `json:"team_id" binding:"required"`
	StateCode    string         `json:"state_code"`
	Comment      string         `json:"comment"`
}

// 添加任务, 请求处理
func postTaskHandler(c *gin.Context) {
	var task_form TaskForm
	// 请求参数数据绑定和校验
	if err := c.Bind(&task_form); err != nil {
		util.NewResult(c).Error(0, err.Error())
		return
	}
	// 判断任务是否存在
	if task_exists := TaskExists(&task_form); task_exists.ID > 0 {
		util.NewResult(c).Success("任务已存在",
			map[string]interface{}{"id": task_exists.ID})
		return
	}
	// 添加任务
	if id := CreateTask(&task_form); id > 0 {
		util.NewResult(c).Success("任务已存在", map[string]interface{}{"id": id})
		return
	}
	util.NewResult(c).Error(0, "添加任务失败")
}
